Adjust the style to the project's taste after a review.

Akinori MUSHA vor 11 Jahren
Ursprung
Commit
fdc39ec425
1 geänderte Dateien mit 36 neuen Zeilen und 38 gelöschten Zeilen
  1. 36 38
      app/models/agents/event_formatting_agent.rb

+ 36 - 38
app/models/agents/event_formatting_agent.rb

@@ -82,43 +82,7 @@ module Agents
82 82
     def validate_options
83 83
       errors.add(:base, "instructions, mode, skip_agent, and skip_created_at all need to be present.") unless options['instructions'].present? and options['mode'].present? and options['skip_agent'].present? and options['skip_created_at'].present?
84 84
 
85
-      case matchers = options['matchers']
86
-      when nil
87
-      when Array
88
-        matchers.each do |matcher|
89
-          unless Hash === matcher
90
-            errors.add(:base, "each matcher must be a hash")
91
-            next
92
-          end
93
-
94
-          regexp, path, to = matcher.values_at(*%w[regexp path to])
95
-
96
-          case regexp
97
-          when String
98
-            begin
99
-              Regexp.new(regexp)
100
-            rescue
101
-              errors.add(:base, "bad regexp found in matchers: #{regexp}")
102
-            end
103
-          else
104
-            errors.add(:base, "regexp is mandatory for a matcher and must be a string")
105
-          end
106
-
107
-          case path
108
-          when String
109
-          else
110
-            errors.add(:base, "path is mandatory for a matcher and must be a string")
111
-          end
112
-
113
-          case to
114
-          when nil, String
115
-          else
116
-            errors.add(:base, "to must be a string if present in a matcher")
117
-          end
118
-        end
119
-      else
120
-        errors.add(:base, "matchers must be an array if present")
121
-      end
85
+      validate_matchers
122 86
     end
123 87
 
124 88
     def default_options
@@ -149,6 +113,40 @@ module Agents
149 113
       end
150 114
     end
151 115
 
116
+    private
117
+
118
+    def validate_matchers
119
+      matchers = options['matchers'] or return
120
+
121
+      unless matchers.is_a?(Array)
122
+        errors.add(:base, "matchers must be an array if present")
123
+        return
124
+      end
125
+
126
+      matchers.each do |matcher|
127
+        unless matcher.is_a?(Hash)
128
+          errors.add(:base, "each matcher must be a hash")
129
+          next
130
+        end
131
+
132
+        regexp, path, to = matcher.values_at(*%w[regexp path to])
133
+
134
+        if regexp.present?
135
+          begin
136
+            Regexp.new(regexp)
137
+          rescue
138
+            errors.add(:base, "bad regexp found in matchers: #{regexp}")
139
+          end
140
+        else
141
+          errors.add(:base, "regexp is mandatory for a matcher and must be a string")
142
+        end
143
+
144
+        errors.add(:base, "path is mandatory for a matcher and must be a string") if !path.present?
145
+
146
+        errors.add(:base, "to must be a string if present in a matcher") if to.present? && !to.is_a?(String)
147
+      end
148
+    end
149
+
152 150
     def perform_matching(payload)
153 151
       matchers.inject(payload.dup) { |hash, matcher|
154 152
         matcher[hash]
@@ -164,7 +162,7 @@ module Agents
164 162
             proc { |hash|
165 163
               mhash = {}
166 164
               value = Utils.value_at(hash, path)
167
-              if String === value and m = re.match(value)
165
+              if value.is_a?(String) && (m = re.match(value))
168 166
                 m.to_a.each_with_index { |s, i|
169 167
                   mhash[i.to_s] = s
170 168
                 }